package servlet;


import entity.Employee;
import entity.Freelancer;
import service.EmployeeService;
import service.FreelancerService;
import service.Impl.EmployeeServiceImpl;
import service.Impl.FreelancerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/Freelancer")
public class FreelancerServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method == null) {
            method = "findAll_freelancer";
        }
        switch (method) {
            // 查询所有自由职业者信息
            case "findAll_freelancer":
                FreelancerService showFreelancerService = new FreelancerServiceImpl();
                ArrayList<Freelancer> freelancerList = null;
                freelancerList = showFreelancerService.getFreelancerList();
                req.setAttribute("FreelancerList", freelancerList);
                req.getRequestDispatcher("jsp/freelancer.jsp").forward(req, resp);
                break;
            // 接受添加请求，跳转到添加自由职业者界面
            case "add":
                resp.sendRedirect("jsp/addFreelancer.jsp");
                break;
            //添加
            case "addFreelancer":
                Integer fid = Integer.valueOf(req.getParameter("freelancerId"));
                String name = req.getParameter("freelancerName");
                Integer age = Integer.valueOf(req.getParameter("age"));
                String job = req.getParameter("job");
                String addr = req.getParameter("addr");
                String phone = req.getParameter("phone");
                String wechat = req.getParameter("wechat");
                String mail = req.getParameter("mail");
                Freelancer freelancer = new Freelancer(fid,name,age,job,addr,phone,wechat,mail);
                FreelancerService freelancerService = new FreelancerServiceImpl();
                //判断是否添加成功
                boolean isAdd = freelancerService.addFreelancer(freelancer);
                // 如果成功添加，返回自由职业者列表界面
                if (isAdd) {
                    resp.sendRedirect("Freelancer");
                }else { // 修改失败，跳转到错误界面
                    resp.sendRedirect("jsp/error.jsp");
                }
                break;
            // 接受更新请求，跳转到更改界面
            case "update":
                Integer freelancerId = Integer.valueOf(req.getParameter("freelancerId"));
                FreelancerService freelancerService1 = new FreelancerServiceImpl();
                Freelancer freelancer1 = freelancerService1.findById(freelancerId);
                req.setAttribute("freelancer", freelancer1);
                req.getRequestDispatcher("jsp/updateFreelancer.jsp").forward(req,resp);
                break;
            // 修改某个自由职业者信息
            case "updateFreelancer":
                // 获取自由职业者信息
                fid = Integer.valueOf(req.getParameter("freelancerId"));
                name = req.getParameter("freelancerName");
                age = Integer.valueOf(req.getParameter("age"));
                job = req.getParameter("job");
                addr = req.getParameter("addr");
                phone = req.getParameter("phone");
                wechat = req.getParameter("wechat");
                mail = req.getParameter("mail");
                Freelancer freelancer2 = new Freelancer(fid,name,age,job,addr,phone,wechat,mail);
                FreelancerService freelancerService2 = new FreelancerServiceImpl();
                // 判断是否修改成功
                boolean isUpdate = freelancerService2.updateFreelancer(freelancer2);
                // 如果成功修改，返回自由职业者列表界面
                if (isUpdate) {
                    resp.sendRedirect("Freelancer");
                }else { // 修改失败，跳转到错误界面
                    resp.sendRedirect("jsp/error.jsp");
                }
                break;
            // 根据自由职业者编号删除自由职业者信息
            case "delete":
                Integer freelancerId2 = Integer.valueOf(req.getParameter("freelancerId"));
                FreelancerService freelancerService3 = new FreelancerServiceImpl();
                boolean isDelete = freelancerService3.deleteFreelancer(freelancerId2);
                if (isDelete) {
                    resp.sendRedirect("Freelancer");
                } else {
                    resp.sendRedirect("jsp/error.jsp");
                }
                break;


            default:
                break;
        }

    }

}
